<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class ArticleController extends AdminbaseController {
	

	
	function _initialize() {
		parent::_initialize();
		$this->db = D("Article");
		$this->cate = D("Cate");
		$this->model="Article";
		//$this->terms_relationship = D("Common/TermRelationships");
	}
	function index(){
		
		$order=I('get.order');
		
		$order_array=array("top"=>"DESC","listorder" => "DESC","updatetime"=>"DESC","id"=>"DESC");
		
		if($order){
			$order_array=array($order=>"DESC");
			
		}
		
		$starttime=strtotime(I('get.start_time'));
		
		$endtime=strtotime(I('get.end_time'));
		
		if($starttime&&$endtime&&($starttime>$endtime)){
			$this->error("开始时间不能大于结束时间");
			
		}
		$catid=I('get.catid');
		
		if(!$catid){
		$this->error("缺少参数");
			
		}
		
		$cat=$this->cate->where(array("id"=>$catid))->find();
		
		if(!$cat){
			$this->error("该栏目不存在");
		}else{
			
			$this->assign("cate",$cat);
		}
		
		cookie("list_cat",$catid);
		
		
		if(I("get.keyword")){
			
			$map['title']=array('like','%'.I("get.keyword").'%');
			
		}
		if($starttime){
			$map['updatetime']  =array('gt',$starttime);
			
		}
		
		if($endtime){
			$map['updatetime']  =array('lt',$endtime);
				
		}
		
		$map['status']  ="1";
		$map['catid']=$catid;
	
		
		$count=$this->db->where($map)->count();
		$page = $this->page($count, 20);
		$list=$this->db->where($map)->limit($page->firstRow . ',' . $page->listRows)->order ( $order_array)->select();
		
		$this->assign("Page",$page->show(""));
		
		$this->assign("list",$list);
		
		$this->display();
	}
	
	function add(){
		
		
		if(IS_POST){
			$_POST['model']=$this->model;
                if ($this->db->add_data($_POST)) {
                
                    $this->success("添加成功！", U("index",array("catid"=>$_POST[catid])));
                } else {
                    $error = $this->db->getError();
                    $this->error($error ? $error : '添加失败！');
                }
            
			
			
		}else{
			
		
			
			$catid=I('get.catid');
			
			if(!$catid){
				$this->error("缺少参数");
					
			}
			
			$cat=$this->cate->where(array("id"=>$catid))->find();
			
			if(!$cat){
				$this->error("该栏目不存在");
			}else{
					
				$this->assign("cate",$cat);
			}
			
			
	
			$this->display("form");
			
		}
		

	
	}
	
	
	public function daoru(){
		
		for ($x=0; $x<=20; $x++) {
		$data=array();
		$data[title]="办事流程".$x;
		$data[content]='办事流程';
		$data[status]=1;
		$data[model]='Article';
		$data[author]="管理员";
		$data[catid]="68";
		$this->db->add_data($data);
	}
// 		$list=M("ArticleCopy")->select();
// 		foreach ($list as $key =>$v){
// 			$data=array();
// 			$data[title]=$v[title];
// 			$data[content]=$v[content];
// 			$data[status]=1;
// 			$data[model]='Article';
// 			$data[author]="管理员";
			
			
// 			if($v[type_id]==2) $data[catid]="57";
// 			if($v[type_id]==3) $data[catid]="81";
// 			if($v[type_id]==4) $data[catid]="45";
// 			if($v[type_id]==5) $data[catid]="59";
// 			if($v[type_id]==6) $data[catid]="60";
// 			if($v[type_id]==19) $data[catid]="58";
			
// 			if(!$data['catid'])
// 				continue;
			
// 		//	print_r($data);
// 			$this->db->add_data($data);
		
			
	
		//echo 1;
		
		
	}
	

	
	public function edit(){
	
	if(IS_POST){
			$_POST['model']=$this->model;
                if ($this->db->edit_data($_POST)) {
                    
                    $this->success("添加成功！",  U("index",array("catid"=>$_POST[catid])));
                } else {
                    $error = $this->db->getError();
                    $this->error($error ? $error : '添加失败！');
                }
            
			
			
		}else{
		$id=  intval(I("get.id"));
		
		
	
		$rs=$this->db->where("id=$id")->find();
		
		$cat=$this->cate->where(array("id"=>$rs[catid]))->find();
		
		
		$this->assign("cate",$cat);
	
		
		
		$this->assign("data",$rs);
		$this->assign("edit",true);
		//$this->assign ( "select_categorys", $this->cate->get_article_tree ($rs['catid']) );
		$this->display("form");
		}
	}
	
	

	
	

	//排序
	public function listorders() {
		$status = parent::_listorders($this->db);
		if ($status) {
			$this->success("排序更新成功！");
		} else {
			$this->error("排序更新失败！");
		}
	}
	
	function delete(){
		if(isset($_GET['id'])){
			$id = intval(I("get.id"));
	
			$rs=$this->db->where(array("id"=>$id))->getField("thumb");
			if ($this->db->delete($id)) {
	
				D('File')->del_file($rs);
	
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}
		if(isset($_POST['ids'])){
			$ids=I("param.ids");
	
			$map[id]=array("in",$ids);
	
			if ($this->db->where($map)->delete()) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}
	}
	
	

	

	
	
}